﻿using System;
using Teigha.Core;

namespace OdaOperation.Core.Simplifier
{
    internal class SimplifierVectorizer : OdGsBaseVectorizer
    {
        private OdGiGeometrySimplifier m_geometrySimplifier;

        private SimplifierVectorizeView m_view;

        private SimpliferVectorizeDevice m_device;

        private OdGiClipBoundary m_eyeClip = new OdGiClipBoundary();

        public SimplifierVectorizer()
        {
            MemoryTransaction currentTransaction = MemoryManager.GetMemoryManager().GetCurrentTransaction();
            if (currentTransaction != null)
            {
                currentTransaction.AddObject(this);
            }
            this.m_eyeClip.m_bDrawBoundary = false;
        }

        internal void SetGeometrySimplifier(OdGiGeometrySimplifier geometry)
        {
            this.m_geometrySimplifier = geometry;
            this.output().setDestGeometry(geometry);
        }

        internal void SetView(SimplifierVectorizeView view)
        {
            this.setUp(view);
            this.m_view = view;
            view.SetVectorizer(this);
        }

        public override void setUp(OdGsViewImpl view)
        {
            view.clearFrozenLayers();
            base.setUp(view);
        }

        public override void beginViewVectorization()
        {
            base.beginViewVectorization();
            this.m_geometrySimplifier.setDeviation(base.drawContext().eyeDeviation());
            base.setDrawContextFlags(this.drawContextFlags(), false);
        }

        public override void draw(OdGiDrawable pDrawable)
        {
            MemoryTransaction value = MemoryManager.GetMemoryManager().StartTransaction();
            try
            {
                base.draw(pDrawable);
            }
            catch (Exception)
            {
            }
            MemoryManager.GetMemoryManager().StopTransaction(value);
        }

        public override bool regenAbort()
        {
            return false;
        }

        public override void updateViewport()
        {
            MemoryTransaction value = MemoryManager.GetMemoryManager().StartTransaction();
            this.m_geometrySimplifier.setDrawContext(base.drawContext());
            OdGeMatrix3d odGeMatrix3d = this.m_view.eyeToScreenMatrix();
            OdGeMatrix3d odGeMatrix3d2 = odGeMatrix3d.inverse();
            base.setEyeToOutputTransform(odGeMatrix3d);
            this.m_eyeClip.m_bClippingFront = this.m_view.isFrontClipped();
            this.m_eyeClip.m_bClippingBack = this.m_view.isBackClipped();
            this.m_eyeClip.m_dFrontClipZ = this.m_view.frontClip();
            this.m_eyeClip.m_dBackClipZ = this.m_view.backClip();
            this.m_eyeClip.m_vNormal = this.viewDir();
            this.m_eyeClip.m_ptPoint = this.m_view.target();
            this.m_eyeClip.m_Points.Clear();
            OdGeVector2d vect = new OdGeVector2d(this.m_view.fieldWidth() / 2.0, this.m_view.fieldHeight() / 2.0);
            this.m_eyeClip.m_Points.Add(OdGePoint2d.kOrigin.Sub(vect));
            this.m_eyeClip.m_Points.Add(OdGePoint2d.kOrigin.Add(vect));
            this.m_eyeClip.m_xToClipSpace = this.getWorldToEyeTransform();
            this.pushClipBoundary(this.m_eyeClip);
            base.updateViewport();
            this.popClipBoundary();
            MemoryManager.GetMemoryManager().StopTransaction(value);
        }

        internal void SetDevice(SimpliferVectorizeDevice simpliferVectorizeDevice)
        {
            this.m_device = simpliferVectorizeDevice;
        }
    }
}
